﻿using System;
using HVACWebService.DataAccese.Entity;
using System.Collections.Generic;
using System.Linq;

namespace HVACWebService.DataAccese.DataService
{
    public class CustomerDeviceDataAccess
    {
        /// <summary>
        /// 新建用户绑定
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="deviceId"></param>
        /// <param name="ableDeivceId"></param>
        /// <param name="deviceName"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="address"></param>
        /// <param name="bindState"></param>
        /// <returns></returns>
        public int CreateCustomerDevice(string openId, string deviceId, string ableDeivceId, string deviceName,
            string phoneNumber, string address, int bindState)
        {
            var hvacDatabaseEntities = new HvacDatabaseEntities();

            var customerDevice = new CustomerDevice
            {
                OpenId = openId,
                DeviceId = deviceId,
                AbleDeivceId = ableDeivceId,
                DeviceName = deviceName,
                PhoneNumber = phoneNumber,
                Address = address,
                BindState = bindState,
                CreateTime = DateTime.Now,
                LastTime = DateTime.Now
            };
            hvacDatabaseEntities.CustomerDevice.Add(customerDevice);
            return hvacDatabaseEntities.SaveChanges();
        }

        /// <summary>
        /// 更新绑定状态
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="deviceId"></param>
        /// <param name="bindState"></param>
        /// <param name="ableDeviceId"></param>
        /// <returns></returns>
        public int UpdateBindState(string openId, string deviceId, int bindState, string ableDeviceId)
        {
            var hvacDatabaseEntities = new HvacDatabaseEntities();

            var updateCustomerDevice =
                hvacDatabaseEntities.CustomerDevice.FirstOrDefault(cd => cd.OpenId == openId && cd.DeviceId == deviceId);
            if (updateCustomerDevice != null)
            {

                if (bindState != 99)
                {
                    updateCustomerDevice.BindState = bindState;
                }

                if (ableDeviceId != "0")
                {
                    updateCustomerDevice.AbleDeivceId = ableDeviceId;
                }
                updateCustomerDevice.LastTime =DateTime.Now;
                return hvacDatabaseEntities.SaveChanges();
            }
            return 0;
        }

        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public CustomerDevice GetCustomerDeviceByOpenIdAndDeviceId(string openId, string deviceId)
        {
            var hvacDatabaseEntities = new HvacDatabaseEntities();
            return hvacDatabaseEntities.CustomerDevice.FirstOrDefault(cd => cd.OpenId == openId && cd.DeviceId == deviceId && cd.BindState==1);
        }

        /// <summary>
        /// 获取用户设备列表
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public List<CustomerDevice> GetCustomerDeviceByOpenId(string openId)
        {
            var hvacDatabaseEntities = new HvacDatabaseEntities();
            return hvacDatabaseEntities.CustomerDevice.Where(cd => cd.OpenId == openId && cd.BindState==1).ToList();
        }

        /// <summary>
        /// 获取用户用户绑定设备信息
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public CustomerDevice GetCustomerDeviceBindByOpenIdAndDeviceId(string openId, string deviceId)
        {
            var hvacDatabaseEntities = new HvacDatabaseEntities();
            return hvacDatabaseEntities.CustomerDevice.FirstOrDefault(cd => cd.OpenId == openId && cd.DeviceId == deviceId && cd.BindState == 0);
        }

    }
}